package com.listen.common;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

/**
 * <p>Title:MessageBase64</p>
 * <p>Description: 对数据进行base64编码和解码的工具类</p>
 * @author xiongxiaoyu
 * @date 2016年8月19日
 */
public class MessageBase64 {
	/**
	 * 编码
	 * @param bstr
	 * @return String
	 */
	public static String encode(String str) {
		byte[] encodeBase64 = null;
		try {
			encodeBase64 = Base64.encodeBase64(str.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return new String(encodeBase64);
	}

	/**
	 * 解码
	 * @param str
	 * @return string
	 */
	public static String decode(String str) {
		byte[] decodeBase64 = null;
		try {
			decodeBase64 = Base64.decodeBase64(str.getBytes());  
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new String(decodeBase64);
	}
	
	public static void main(String args[]) throws UnsupportedEncodingException{
		System.out.println(encode("singleLogin"));
		/**
		 *  6
		 *	1
	 	 *  sql_sel_title
		 *  QFSOFT!@#$&*
		 */
		/*System.out.println(decode("Ng=="));
		System.out.println(decode("MQ=="));
		System.out.println(decode("c3FsX3NlbF90aXRsZQ=="));
		System.out.println(decode("UUZTT0ZUIUAjJCYq"));*/
		
		System.out.println(decode("Mzc="));//37
		System.out.println(decode("MQ=="));
		System.out.println(decode("c3FsX3NlbF90aXRsZQ=="));
		System.out.println(decode("UUZTT0ZUIUAjJCYq"));
		
	}
}